package com.hbnu.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * 需求：用户登录
 *
 * @author kittychen
 * @since 2025-10-20 20:15
 */
public class JdbcDemo04 {

    public static void main(String[] args) {
        // 1、给用户提供登录的界面（入口）
        Map<String, String> userInfo = initUI();

        // 2、校验用户输入的账号和密码
        boolean result = verifiedUserInfo(userInfo);

        System.out.println(result ? "登录成功" : "登录失败");
    }

    private static boolean verifiedUserInfo(Map<String, String> userInfo) {
        boolean flag = false;

        String username = userInfo.get("username");
        String password = userInfo.get("password");

        Connection connection = null;
        // Statement statement = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            String url = "jdbc:mysql://127.0.0.1:3306/class2309?serverTimezone=GMT&useSSL=false";
            String user = "root";
            String pwd = "123456";
            connection = DriverManager.getConnection(url, user, pwd);

            // statement = connection.createStatement();

            // String sql = "select * from tb_user where username = '" + username + "' and password = '" + password + "'";
            String sql = "select * from tb_user where username = ? and password = ?";   // ?表示占位符，下标从1开始
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                flag = true;
                return flag;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 6、关闭资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return flag;
    }

    private static Map<String, String> initUI() {
        Map<String, String> userInfo = new HashMap<>();

        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入用户名：");
        String username = scanner.nextLine();
        System.out.println("请输入密码：");
        String password = scanner.nextLine();

        userInfo.put("username", username);
        userInfo.put("password", password);

        return userInfo;
    }
}
